//
//  AssortSelectViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/9/26.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "AssortSelectViewController.h"
#import "RentVieitor.h"
#import "TypeCell.h"

@interface AssortSelectViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation AssortSelectViewController {
    
    NSString *_identify;
    UITableView *_tableview;
    NSMutableArray *selectArray;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.titleName;
    self.view.backgroundColor = BackgroundColor;
    selectArray = [NSMutableArray array];
    [self creatLeftView];
    [self creatView];
}

- (void)returnAssortBlock:(SelectAssortBlock)block {
    
    self.selecAssortBlock = block;
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(-5, 0, 10, 15);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(0, 0, 50, 20);
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)creatView {
    
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight) style:UITableViewStylePlain];
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview .showsHorizontalScrollIndicator = NO;
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _identify = @"_Cell";
    _tableview.editing = YES;
    _tableview.allowsMultipleSelectionDuringEditing = YES;
    _tableview.backgroundColor = [UIColor clearColor];
    _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [_tableview registerClass:[TypeCell class] forCellReuseIdentifier:_identify];
    [self.view addSubview:_tableview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.data.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TypeCell *cell = [tableView dequeueReusableCellWithIdentifier:_identify forIndexPath:indexPath];
    cell.tintColor = [Utils colorConvertFromString:@"#1fae0c"];
    cell.text = self.data[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
     [selectArray addObject:self.data[indexPath.row]];
}



- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (void)doneAction {
    
    if (selectArray.count == 0) {
        
        [APPDELEGATE showPro:@"请选择配套设施"];
        return;
    }
    
    NSString *jsonStr = [self arrayToJSONString:selectArray];
    if (self.selecAssortBlock != nil) {
        
        self.selecAssortBlock(jsonStr);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSString *)arrayToJSONString:(NSArray *)array
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}
@end
